package github.ziwukun.jsonql.lang;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;

public class LimitBuilder implements Serializable{
	private static final long serialVersionUID = 742020086175773478L;
	
	String model;
	Integer size;
	Integer offset;
	Integer page;
	List<LimitBuilder> childs = new LinkedList<>();
	LimitBuilder(){}
	
	public LimitBuilder model(String model) {
		this.model = model;
		return this;
	}
	
	public LimitBuilder size(Integer size) {
		this.size = size;
		return this;
	}
	
	public LimitBuilder offset(Integer offset) {
		this.offset = offset;
		return this;
	}
	
	public LimitBuilder page(Integer page) {
		this.page = page;
		return this;
	}
	
	public LimitBuilder prop(String model,Consumer<LimitBuilder> consumer) {
		LimitBuilder propLimit = new LimitBuilder();
		propLimit.model = model;
		consumer.accept(propLimit);
		
		this.childs.add(propLimit);
		return this;
	}
	
	public Limit build() {
		List<Limit> childs = new LinkedList<>();
		for (LimitBuilder childBuilder : this.childs) {
			childs.add(childBuilder.build());
		}
		
		return new LimitImpl(model, size, offset, page, childs);
	}
}
